﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{

    [HandleError]
    public class AccountController : Controller
    {
    
        protected override void Initialize(RequestContext requestContext)
        {
            
            base.Initialize(requestContext);
        }

        // **************************************
        // URL: /Account/LogOn
        // **************************************

        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
 
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                UserInfoOperate uio = new UserInfoOperate();
                UserInfo userinfo=uio.ValidateUser(model.UserName,model.Password);
                if (userinfo!=null)
               {
                   FormsAuthentication.SetAuthCookie(model.UserName,model.RememberMe);
                   Session["blogId"] = userinfo.userId;
                   if (!String.IsNullOrEmpty(returnUrl))
                  {
                        return Redirect(returnUrl);
                   }
                   else
                   {
                        return RedirectToAction("Index", "Home");
                   }
                }
                else
            {
                    ModelState.AddModelError("", "用户名或密码不正确");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public ActionResult logOnUser() {
            if (User.Identity.Name != null)
            {
                if (Session["blogId"] == null)
                {
                    UserInfoOperate uio = new UserInfoOperate();
                    Session["blogId"] = uio.GetUserIdByUserEmail(User.Identity.Name);

                }
            }
            return PartialView("LogOnUserControl");
        } 



        public ActionResult LogOff()
        {
            FormsAuthentication .SignOut();

            return RedirectToAction("Index", "Home");
        }

        /// <summary>
        ///注册
        /// </summary>
        /// <returns></returns>
        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user

                UserInfoOperate uio = new UserInfoOperate();
                Blog blog = new Blog();
                blog.blogName = model.BlogName;
                UserInfo userinfo = new UserInfo();
                userinfo.userEmail = model.UserName;
                userinfo.userPassword = model.Password;
                userinfo.userName = model.NickName;
                if (uio.Register(userinfo))
                {
                    int bolgid = userinfo.userId;
                    if ( bolgid != 0)
                    {
                        blog.blogId=bolgid;
                        if(uio.InsertBlog(blog))
                        {
                            FormsAuthentication.SetAuthCookie(model.UserName,false);
                            return RedirectToAction("index","home");

                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("", "十分抱歉，注册并未成功请稍后尝试");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }


        // **************************************
        // URL: /Account/Register
        // **************************************
 
               public ActionResult AddField()
        {
            return View();
        }

        public ActionResult test()
        {
            return View();
        }
        public ActionResult GetValidateCode()
        {
            ValidateCode vcode = new ValidateCode();
            string code = vcode.CreateValidateCode(5);
            Session["ValidateCode"] = code;
            byte[] bytes = vcode.CreateValidateGraphic(code);
            return File(bytes, @"image/jpeg");
        }
        public ActionResult FieldUser()
        {
            List<string> name=new List<string>();
            name.Add("齐白石的博客");
            name.Add("石头记");
            List<string> field=new List<string>();
            field.Add("美术");
            field.Add("摆饰");
            ViewData["name"] = name;
            ViewData["fild"] =field;
            return PartialView();
        }

    }
}
